using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class UserCardRecordDal
	{
		public bool Exists(long id)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from MBF_UserCardRecord");
			stringBuilder.Append(" where ");
			stringBuilder.Append("id = @id ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@id", SqlDbType.BigInt)
			};
			array[0].Value = id;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public long Add(UserCardRecordModel model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into MBF_UserCardRecord(");
			stringBuilder.Append("UserID,CardType,CardNumber,Amount,UserTime,Remark");
			stringBuilder.Append(") values (");
			stringBuilder.Append("@UserID,@CardType,@CardNumber,@Amount,@UserTime,@Remark");
			stringBuilder.Append(") ");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@UserID", SqlDbType.Int, 4),
				new SqlParameter("@CardType", SqlDbType.NVarChar, 50),
				new SqlParameter("@CardNumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@Amount", SqlDbType.Decimal, 9),
				new SqlParameter("@UserTime", SqlDbType.DateTime),
				new SqlParameter("@Remark", SqlDbType.Text)
			};
			array[0].Value = model.UserID;
			array[1].Value = model.CardType;
			array[2].Value = model.CardNumber;
			array[3].Value = model.Amount;
			array[4].Value = model.UserTime;
			array[5].Value = model.Remark;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			long result;
			if (single == null)
			{
				result = 0L;
			}
			else
			{
				result = System.Convert.ToInt64(single);
			}
			return result;
		}
		public bool Update(UserCardRecordModel model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update MBF_UserCardRecord set ");
			stringBuilder.Append(" UserID = @UserID , ");
			stringBuilder.Append(" CardType = @CardType , ");
			stringBuilder.Append(" CardNumber = @CardNumber , ");
			stringBuilder.Append(" Amount = @Amount , ");
			stringBuilder.Append(" UserTime = @UserTime , ");
			stringBuilder.Append(" Remark = @Remark  ");
			stringBuilder.Append(" where id=@id ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@id", SqlDbType.BigInt, 8),
				new SqlParameter("@UserID", SqlDbType.Int, 4),
				new SqlParameter("@CardType", SqlDbType.NVarChar, 50),
				new SqlParameter("@CardNumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@Amount", SqlDbType.Decimal, 9),
				new SqlParameter("@UserTime", SqlDbType.DateTime),
				new SqlParameter("@Remark", SqlDbType.Text)
			};
			array[0].Value = model.id;
			array[1].Value = model.UserID;
			array[2].Value = model.CardType;
			array[3].Value = model.CardNumber;
			array[4].Value = model.Amount;
			array[5].Value = model.UserTime;
			array[6].Value = model.Remark;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(long id)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from MBF_UserCardRecord ");
			stringBuilder.Append(" where id=@id");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@id", SqlDbType.BigInt)
			};
			array[0].Value = id;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string idlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from MBF_UserCardRecord ");
			stringBuilder.Append(" where ID in (" + idlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public UserCardRecordModel GetModel(long id)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select id, UserID, CardType, CardNumber, Amount, UserTime, Remark  ");
			stringBuilder.Append("  from MBF_UserCardRecord ");
			stringBuilder.Append(" where id=@id");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@id", SqlDbType.BigInt)
			};
			array[0].Value = id;
			UserCardRecordModel userCardRecordModel = new UserCardRecordModel();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			UserCardRecordModel result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				if (dataSet.Tables[0].Rows[0]["id"] != null && dataSet.Tables[0].Rows[0]["id"].ToString() != "")
				{
					userCardRecordModel.id = new long?(long.Parse(dataSet.Tables[0].Rows[0]["id"].ToString()));
				}
				if (dataSet.Tables[0].Rows[0]["UserID"] != null && dataSet.Tables[0].Rows[0]["UserID"].ToString() != "")
				{
					userCardRecordModel.UserID = new int?(int.Parse(dataSet.Tables[0].Rows[0]["UserID"].ToString()));
				}
				userCardRecordModel.CardType = dataSet.Tables[0].Rows[0]["CardType"].ToString();
				userCardRecordModel.CardNumber = dataSet.Tables[0].Rows[0]["CardNumber"].ToString();
				if (dataSet.Tables[0].Rows[0]["Amount"] != null && dataSet.Tables[0].Rows[0]["Amount"].ToString() != "")
				{
					userCardRecordModel.Amount = new decimal?(decimal.Parse(dataSet.Tables[0].Rows[0]["Amount"].ToString()));
				}
				if (dataSet.Tables[0].Rows[0]["UserTime"] != null && dataSet.Tables[0].Rows[0]["UserTime"].ToString() != "")
				{
					userCardRecordModel.UserTime = new System.DateTime?(System.DateTime.Parse(dataSet.Tables[0].Rows[0]["UserTime"].ToString()));
				}
				userCardRecordModel.Remark = dataSet.Tables[0].Rows[0]["Remark"].ToString();
				result = userCardRecordModel;
			}
			else
			{
				result = null;
			}
			return result;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select * ");
			stringBuilder.Append(" FROM MBF_UserCardRecord ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string fieldOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" * ");
			stringBuilder.Append(" FROM MBF_UserCardRecord ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + fieldOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetListRowid(int Top, string strWhere, string fieldOrder, params string[] selectField)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			string text = "*";
			if (selectField.Length > 0)
			{
				text = selectField[0];
				if (text.Trim() == "")
				{
					text = "*";
				}
			}
			stringBuilder.AppendFormat(" ROW_NUMBER() OVER (ORDER BY id ASC) rowid,{0} ", text);
			stringBuilder.Append(" FROM MBF_UserCardRecord ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + fieldOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetPagedList(int PageIndex, int PageSize, string strWhere, string Order)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.AppendFormat("select  top {0} * from [MBF_UserCardRecord] where ", PageSize);
			if (strWhere.Trim() != "")
			{
				stringBuilder.AppendFormat(" {0} and ", strWhere);
			}
			stringBuilder.AppendFormat(" id not in (select top {0} id from [MBF_UserCardRecord] ", (PageIndex - 1) * PageSize);
			if (strWhere.Trim() != "")
			{
				stringBuilder.AppendFormat(" where {0} ", strWhere);
			}
			if (Order.Trim() != "")
			{
				stringBuilder.Append(" order by  " + Order);
			}
			stringBuilder.Append(")");
			if (Order.Trim() != "")
			{
				stringBuilder.Append(" order by  " + Order);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public long Add(UserCardRecordModel model, params Conn[] conn)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into MBF_UserCardRecord(");
			stringBuilder.Append("UserID,CardType,CardNumber,Amount,UserTime,Remark");
			stringBuilder.Append(") values (");
			stringBuilder.Append("@UserID,@CardType,@CardNumber,@Amount,@UserTime,@Remark");
			stringBuilder.Append(") ");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@UserID", SqlDbType.Int, 4),
				new SqlParameter("@CardType", SqlDbType.NVarChar, 50),
				new SqlParameter("@CardNumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@Amount", SqlDbType.Decimal, 9),
				new SqlParameter("@UserTime", SqlDbType.DateTime),
				new SqlParameter("@Remark", SqlDbType.Text)
			};
			array[0].Value = model.UserID;
			array[1].Value = model.CardType;
			array[2].Value = model.CardNumber;
			array[3].Value = model.Amount;
			array[4].Value = model.UserTime;
			array[5].Value = model.Remark;
			object obj;
			if (conn.Length > 0)
			{
				obj = conn[0].ExecuteTransactionScalar(stringBuilder.ToString(), array);
			}
			else
			{
				obj = Conn.GetSingle(stringBuilder.ToString(), array);
			}
			long result;
			if (obj == null)
			{
				result = 0L;
			}
			else
			{
				result = System.Convert.ToInt64(obj);
			}
			return result;
		}
	}
}
